<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org" xmlns:sec="http://www.thymeleaf.org/extras/spring-security">
<head>
    <th:block th:include="include :: header('辞海（命名规范）')"/>
</head>
<body class="pear-container">
    <!-- 查询栏 -->
    <div class="layui-card">
        <div class="layui-card-body">
            <form class="layui-form search-form" id="searchForm">
                <div class="layui-form-item">
                    <label class="layui-form-label">中文名称</label>
                    <div class="layui-input-inline">
                        <input type="text" name="cnName" placeholder="" class="layui-input">
                    </div>
                    <label class="layui-form-label">英文名称</label>
                    <div class="layui-input-inline">
                        <input type="text" name="enName" placeholder="" class="layui-input">
                    </div>
                    <button class="pear-btn pear-btn-md pear-btn-primary" lay-submit lay-filter="data-query">
                        <i class="layui-icon layui-icon-search"></i>
                        查询
                    </button>
                    <button type="reset" class="pear-btn pear-btn-md">
                        <i class="layui-icon layui-icon-refresh"></i>
                        重置
                    </button>
                </div>
            </form>
        </div>
    </div>

    <!-- 数据表头操作按钮 -->
    <script type="text/html" id="data-toolbar">
        <button class="pear-btn pear-btn-primary pear-btn-md" lay-event="add">
            <i class="layui-icon layui-icon-add-1"></i>
            新增
        </button>
    </script>

    <!-- 数据列表 -->
    <div class="layui-card">
        <div class="layui-card-body">
            <table id="data-table" lay-filter="data-table"></table>
        </div>
    </div>
</body>

<!-- 列表操作按钮 -->
<script type="text/html" id="operate-bar">
    <button class="pear-btn pear-btn-primary pear-btn-sm" lay-event="edit">
        <i class="layui-icon layui-icon-edit"></i>
    </button>
    <button class="pear-btn pear-btn-danger pear-btn-sm" lay-event="remove">
        <i class="layui-icon layui-icon-delete"></i>
    </button>
</script>

<script type="text/html" id="user-createTime">
    {{layui.util.toDateString(d.createTime, 'yyyy-MM-dd')}}
</script>

<th:block th:include="include :: footer"/>
<script>

    layui.use(['table', 'form', 'jquery','common','popup'], function () {
        let $ = layui.jquery,
            table = layui.table,
            form = layui.form,
            common = layui.common,
            popup = layui.popup;

        // 当前模块根路径
        let prefix = "/standard/ciHai/";

        // 列字段
        let cols = [
            [
                {title: '主键ID', field: 'id', align: 'center', width: 120},
                {title: '中文名称', field: 'cnName', align: 'center'},
                {title: '英文全称', field: 'enFullName', align: 'center'},
                {title: '英文简称', field: 'enName', align: 'center'},
                {title: '中文描述', field: 'cnDescribe', align: 'center'},
                {title: '操作', toolbar: '#operate-bar', align: 'center', width: 130}
            ]
        ]
        // 加载
        table.render({
            elem: '#data-table',
            url: prefix + 'pageList',
            toolbar: '#data-toolbar',
            page: true,
            cols: cols,
            skin: 'line',
            defaultToolbar: [
                'filter', 'exports'
            ]
        });
        // 查询
        form.on('submit(data-query)', function (data) {
            window.refresh(data.field);
            return false;
        });
        // 表单工具按钮栏
        table.on('toolbar(data-table)', function (obj) {
            if (obj.event === 'add') {
                window.add();
            } else if (obj.event === 'refresh') {
                window.refresh();
            }
        });
        // 表单操作按钮
        table.on('tool(data-table)', function (obj) {
            if (obj.event === 'remove') {
                window.remove(obj);
            } else if (obj.event === 'edit') {
                window.edit(obj);
            }
        });
        // 新增窗口
        window.add = function () {
            layer.open({
                type: 2,
                title: '新增',
                shade: 0.1,
                area: ['550px', '500px'],
                content: prefix + 'view'
            });
        }
        // 编辑窗口
        window.edit = function (obj) {
            layer.open({
                type: 2,
                title: '修改',
                shade: 0.1,
                area: ['550px', '500px'],
                content: prefix + 'view?id=' + obj.data['id']
            });
        }
        // 删除窗口
        window.remove = function (obj) {
            layer.confirm('确定要删除该数据？', {icon: 3, title: '提示'}, function (index) {
                layer.close(index);
                let loading = layer.load();
                $.ajax({
                    url: prefix + "remove/" + obj.data['id'],
                    dataType: 'json',
                    type: 'delete',
                    success: function (result) {
                        layer.close(loading);
                        if (result.success) {
                            popup.success(result.msg, function(){
                                table.reload('data-table');
                            });
                        } else {
                            popup.failure(result.msg);
                        }
                    }
                })
            });
        }
        // 刷新
        window.refresh = function (param) {
            table.reload('data-table', {where: param});
        }
    })
</script>
</html>